#ifndef _CRT_RWLOCK_H_
#define _CRT_RWLOCK_H_

#include <crt_core.hpp>

#ifdef __cplusplus
extern "C" {
#endif

#if (TARGET_OS == OS_WIN)

typedef struct {
    int process_shared;
    unsigned int num_readers_;
    CRITICAL_SECTION num_readers_lock_;
    HANDLE write_semaphore_;
} _rwlock_t;

#elif (TARGET_OS == OS_POSIX)

typedef struct {
    int process_shared;
    pthread_rwlock_t rwlock;
} _rwlock_t;

#elif (TARGET_OS == OS_MACH)

typedef struct {
    int process_shared;
    pthread_rwlock_t rwlock;
} _rwlock_t;

#elif (TARGET_OS == OS_UNIX)

typedef struct {
    int process_shared;
    pthread_rwlock_t rwlock;
} _rwlock_t;

#endif

int _rwlock_init(_rwlock_t* lock);
int _rwlock_destroy(_rwlock_t* lock);
int _rwlock_rdlock(_rwlock_t* lock);
int _rwlock_tryrdlock(_rwlock_t* lock);
int _rwlock_rdunlock(_rwlock_t* lock);
int _rwlock_wrlock(_rwlock_t* lock);
int _rwlock_trywrlock(_rwlock_t* lock);
int _rwlock_wrunlock(_rwlock_t* lock);

#ifdef __cplusplus
}
#endif

#endif
